//
//  XLButton.m
//  campus
//
//  Created by xl on 2019/11/11.
//  Copyright © 2019 xl. All rights reserved.
//

#import "XLButton.h"
typedef void (^ButtonAction)(XLButton *);
@interface XLButton ()

@property (nonatomic) CGSize hotZoneSize;
@property (nonatomic, assign) BOOL hasCustHotZone;
@property (nonatomic, copy) ButtonAction buttonAction;

@end
@implementation XLButton

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.adjustsImageWhenHighlighted = NO;
        [self addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(void)buttonClickAction:(XLButton *)button{
    
    if (self.buttonAction) {
        self.buttonAction(button);
    }
}

- (void)setButtonAction:(void (^)(XLButton * button))buttonAction{
    _buttonAction = buttonAction;
}


- (void)setCurrentButtonHotSize:(CGSize)size
{
    self.hasCustHotZone = YES;
    //热区不应小于44x44
    if (size.width < 44) {
        size.width = 44;
    }
    if (size.height < 44) {
        size.height = 44;
    }
    _hotZoneSize = size;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if (self.hasCustHotZone) {
        CGRect bounds = self.bounds;
        CGFloat widthDelta = MAX(self.hotZoneSize.width - bounds.size.width, 0);
        CGFloat heightDelta = MAX(self.hotZoneSize.height - bounds.size.height, 0);
        bounds = CGRectInset(bounds, - 0.5 * widthDelta, - 0.5 * heightDelta);
        return CGRectContainsPoint(bounds, point);
    }
    return [super pointInside:point withEvent:event];
}

- (void)verticalImageAndTitle:(CGFloat)spacing
{
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = self.titleLabel.frame.size;
    CGSize textSize = [self.titleLabel sizeThatFits:CGSizeZero];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    if (titleSize.width + 0.5 < frameSize.width) {
        titleSize.width = frameSize.width;
    }
    CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
    self.imageEdgeInsets = UIEdgeInsetsMake(- (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, - imageSize.width, - (totalHeight - titleSize.height), 0);
    
}

- (void)setButtonAlpha:(CGFloat)alpha
{
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = alpha;
    }];
}

- (void)setNavigationbarType {
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = self.titleLabel.frame.size;
    CGSize textSize = [self.titleLabel sizeThatFits:CGSizeZero];
    CGSize frameSize = CGSizeMake(ceilf(textSize.width), ceilf(textSize.height));
    if (titleSize.width + 0.5 < frameSize.width) {
        titleSize.width = frameSize.width;
    }
    CGFloat totalWidth = (imageSize.width + titleSize.width + 8);
    self.imageEdgeInsets = UIEdgeInsetsMake(0.0, totalWidth - imageSize.width, 0.0, - (totalWidth - imageSize.width));
    self.titleEdgeInsets = UIEdgeInsetsMake(0, - (totalWidth - titleSize.width), - 0, totalWidth - titleSize.width);
}

@end
